{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type='text/css'>\n",
       ".datatable table.frame { margin-bottom: 0; }\n",
       ".datatable table.frame thead { border-bottom: none; }\n",
       ".datatable table.frame tr.coltypes td {  color: #FFFFFF;  line-height: 6px;  padding: 0 0.5em;}\n",
       ".datatable .bool    { background: #DDDD99; }\n",
       ".datatable .object  { background: #565656; }\n",
       ".datatable .int     { background: #5D9E5D; }\n",
       ".datatable .float   { background: #4040CC; }\n",
       ".datatable .str     { background: #CC4040; }\n",
       ".datatable .row_index {  background: var(--jp-border-color3);  border-right: 1px solid var(--jp-border-color0);  color: var(--jp-ui-font-color3);  font-size: 9px;}\n",
       ".datatable .frame tr.coltypes .row_index {  background: var(--jp-border-color0);}\n",
       ".datatable th:nth-child(2) { padding-left: 12px; }\n",
       ".datatable .hellipsis {  color: var(--jp-cell-editor-border-color);}\n",
       ".datatable .vellipsis {  background: var(--jp-layout-color0);  color: var(--jp-cell-editor-border-color);}\n",
       ".datatable .na {  color: var(--jp-cell-editor-border-color);  font-size: 80%;}\n",
       ".datatable .footer { font-size: 9px; }\n",
       ".datatable .frame_dimensions {  background: var(--jp-border-color3);  border-top: 1px solid var(--jp-border-color0);  color: var(--jp-ui-font-color3);  display: inline-block;  opacity: 0.6;  padding: 1px 10px 1px 5px;}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.simplefilter('ignore')\n",
    "\n",
    "import pandas as pd\n",
    "pd.set_option('max_columns', None)\n",
    "pd.set_option('max_rows', 100)\n",
    "pd.set_option('display.float_format',lambda x : '%.2f' % x)\n",
    "import numpy as np\n",
    "np.set_printoptions(suppress=True)\n",
    "from tqdm.notebook import tqdm\n",
    "\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "\n",
    "import gc\n",
    "\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.metrics import f1_score, roc_auc_score\n",
    "\n",
    "import lightgbm as lgb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "raw_data/:\r\n",
      "entprise_evaluate.csv  entprise_submit.csv  train\r\n",
      "\r\n",
      "raw_data/train:\r\n",
      "annual_report_info.csv\tchange_info.csv    news_info.csv   tax_info.csv\r\n",
      "base_info.csv\t\tentprise_info.csv  other_info.csv\r\n"
     ]
    }
   ],
   "source": [
    "!ls -R raw_data/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(14865, 2)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>59b38c56de3836831ff90a77d892a13523b7494f6ed09ff7</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>da8691b210adb3f6be8064e006f220070565db287275ad38</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>82750f1b9d122350918121f97c99bf96e11aa24ee91504a9</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>f000950527a6feb6b2c6de6f85c1e7438ba5590be931e2ec</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f1c1045b13d1832927e3743e49d2917f2d98424f0849a373</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  label\n",
       "0  59b38c56de3836831ff90a77d892a13523b7494f6ed09ff7      1\n",
       "1  da8691b210adb3f6be8064e006f220070565db287275ad38      0\n",
       "2  82750f1b9d122350918121f97c99bf96e11aa24ee91504a9      0\n",
       "3  f000950527a6feb6b2c6de6f85c1e7438ba5590be931e2ec      0\n",
       "4  f1c1045b13d1832927e3743e49d2917f2d98424f0849a373      0"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据集7：entprise_info.csv\n",
    "# 带标注的企业数据。每一行代表一个企业，每一行2列，其中id列为企业唯一标识，label列为标注（1：有非法集资风险，0：无非法集资风险），列之间采用“,”分隔符分割。\n",
    "\n",
    "# 训练集 id 及标签\n",
    "\n",
    "entprise_info = pd.read_csv('raw_data/train/entprise_info.csv')\n",
    "\n",
    "print(entprise_info.shape)\n",
    "entprise_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(10000, 2)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>82750f1b9d1223508ee329d47e27d35176c93eb9f35e9c1a</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>f000950527a6feb670cc1c87c2025f3922aaa4a0206a0a33</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>e9f7b28ec10e04700ef4db75a494f9a1e8e8b09555e6afa1</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>beb4aaaa89e0a0ae9d77bd5d7665be6342f552f51840cf19</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>e9f7b28ec10e0470ee4172cec0133b6826c34f27d3dff204</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  score\n",
       "0  82750f1b9d1223508ee329d47e27d35176c93eb9f35e9c1a    nan\n",
       "1  f000950527a6feb670cc1c87c2025f3922aaa4a0206a0a33    nan\n",
       "2  e9f7b28ec10e04700ef4db75a494f9a1e8e8b09555e6afa1    nan\n",
       "3  beb4aaaa89e0a0ae9d77bd5d7665be6342f552f51840cf19    nan\n",
       "4  e9f7b28ec10e0470ee4172cec0133b6826c34f27d3dff204    nan"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据集8（验证集）：entprise_evaluate.csv\n",
    "# 未标注企业数据。参赛队伍需提交的最终结果数据集，每一行代表一个企业，每一行有 2 列, 其中id列为企业唯一标识，score列为空，列之间采用“,”分隔符分割。\n",
    "\n",
    "# 测试集 id\n",
    "\n",
    "entprise_evaluate = pd.read_csv('raw_data/entprise_evaluate.csv')\n",
    "\n",
    "print(entprise_evaluate.shape)\n",
    "entprise_evaluate.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(24865, 33)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>industryco</th>\n",
       "      <th>dom</th>\n",
       "      <th>opscope</th>\n",
       "      <th>enttype</th>\n",
       "      <th>enttypeitem</th>\n",
       "      <th>opfrom</th>\n",
       "      <th>opto</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>empnum</th>\n",
       "      <th>compform</th>\n",
       "      <th>parnum</th>\n",
       "      <th>exenum</th>\n",
       "      <th>opform</th>\n",
       "      <th>ptbusscope</th>\n",
       "      <th>venind</th>\n",
       "      <th>enttypeminu</th>\n",
       "      <th>midpreindcode</th>\n",
       "      <th>protype</th>\n",
       "      <th>oploc</th>\n",
       "      <th>regcap</th>\n",
       "      <th>reccap</th>\n",
       "      <th>forreccap</th>\n",
       "      <th>forregcap</th>\n",
       "      <th>congro</th>\n",
       "      <th>enttypegb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>340223</td>\n",
       "      <td>M</td>\n",
       "      <td>7513.00</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb24d1db51663c654...</td>\n",
       "      <td>纳米新材料、机械设备、五金配件加工、销售及技术推广服务，道路货物运输。（依法须经批准的项目，...</td>\n",
       "      <td>1100</td>\n",
       "      <td>1150.00</td>\n",
       "      <td>2019-07-11 00:00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340223010010000000</td>\n",
       "      <td>340200000000115392</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>50.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3</td>\n",
       "      <td>340222</td>\n",
       "      <td>O</td>\n",
       "      <td>8090.00</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...</td>\n",
       "      <td>健身服务。（依法须经批准的项目，经相关部门批准后方可开展经营活动）</td>\n",
       "      <td>9600</td>\n",
       "      <td>nan</td>\n",
       "      <td>2017-09-06</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340222060010000000</td>\n",
       "      <td>340200000000112114</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>10</td>\n",
       "      <td>nan</td>\n",
       "      <td>3.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...</td>\n",
       "      <td>10.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>9053.00</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb2ae36cd652943e8...</td>\n",
       "      <td>文化娱乐经纪人服务；境内文艺活动组织与策划；文化艺术交流活动组织策划；演出经纪；其他文化艺术...</td>\n",
       "      <td>1100</td>\n",
       "      <td>1150.00</td>\n",
       "      <td>2020-09-14 14:46:30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340202010010000000</td>\n",
       "      <td>400000000000753910</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>1.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>100.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>340221</td>\n",
       "      <td>L</td>\n",
       "      <td>7212.00</td>\n",
       "      <td>746df9aaed8578571760c563abe882c8ba25209fc6d5db...</td>\n",
       "      <td>投资管理及咨询(证券、期货除外)；企业管理。（依法须经批准的项目，经相关部门批准后方可开展经...</td>\n",
       "      <td>4500</td>\n",
       "      <td>4540.00</td>\n",
       "      <td>2015-09-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340221010010000000</td>\n",
       "      <td>400000000000013538</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>01-以个人财产出资</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>10.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>4540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>8810.00</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb2ae36cd652943e8...</td>\n",
       "      <td>境内文化艺术交流活动策划；企业形象策划；礼仪庆典服务；翻译服务；专利代理；广告设计、制作、代...</td>\n",
       "      <td>1100</td>\n",
       "      <td>1130.00</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2067-11-30</td>\n",
       "      <td>7</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>400000000000283237</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>100.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1130</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  oplocdistrict  \\\n",
       "0  47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4         340223   \n",
       "1  9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3         340222   \n",
       "2  59b38c56de3836838082cfcb1a298951abfe15e6940c49ba         340202   \n",
       "3  e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818         340221   \n",
       "4  f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb         340202   \n",
       "\n",
       "  industryphy  industryco                                                dom  \\\n",
       "0           M     7513.00  31487d8f256f16bd6244b7251be2ebb24d1db51663c654...   \n",
       "1           O     8090.00  31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...   \n",
       "2           R     9053.00  31487d8f256f16bd6244b7251be2ebb2ae36cd652943e8...   \n",
       "3           L     7212.00  746df9aaed8578571760c563abe882c8ba25209fc6d5db...   \n",
       "4           R     8810.00  31487d8f256f16bd6244b7251be2ebb2ae36cd652943e8...   \n",
       "\n",
       "                                             opscope  enttype  enttypeitem  \\\n",
       "0  纳米新材料、机械设备、五金配件加工、销售及技术推广服务，道路货物运输。（依法须经批准的项目，...     1100      1150.00   \n",
       "1                  健身服务。（依法须经批准的项目，经相关部门批准后方可开展经营活动）     9600          nan   \n",
       "2  文化娱乐经纪人服务；境内文艺活动组织与策划；文化艺术交流活动组织策划；演出经纪；其他文化艺术...     1100      1150.00   \n",
       "3  投资管理及咨询(证券、期货除外)；企业管理。（依法须经批准的项目，经相关部门批准后方可开展经...     4500      4540.00   \n",
       "4  境内文化艺术交流活动策划；企业形象策划；礼仪庆典服务；翻译服务；专利代理；广告设计、制作、代...     1100      1130.00   \n",
       "\n",
       "                opfrom        opto  state               orgid  \\\n",
       "0  2019-07-11 00:00:00         NaN      6  340223010010000000   \n",
       "1           2017-09-06         NaN      6  340222060010000000   \n",
       "2  2020-09-14 14:46:30         NaN      6  340202010010000000   \n",
       "3           2015-09-30         NaN      6  340221010010000000   \n",
       "4           2017-12-01  2067-11-30      7  340200000000000000   \n",
       "\n",
       "                jobid  adbusign  townsign  regtype  empnum  compform  parnum  \\\n",
       "0  340200000000115392         0         0        1    5.00       nan     nan   \n",
       "1  340200000000112114         0         1        1    3.00      1.00     nan   \n",
       "2  400000000000753910         0         0        1    2.00       nan    1.00   \n",
       "3  400000000000013538         0         1        1    2.00       nan     nan   \n",
       "4  400000000000283237         0         0        1     nan       nan     nan   \n",
       "\n",
       "   exenum      opform  ptbusscope  venind  enttypeminu  midpreindcode  \\\n",
       "0     nan         NaN         nan     nan      1151.00            nan   \n",
       "1     nan          10         nan    3.00          nan            nan   \n",
       "2     nan         NaN         nan     nan      1151.00            nan   \n",
       "3     nan  01-以个人财产出资         nan     nan          nan            nan   \n",
       "4     nan         NaN         nan     nan          nan            nan   \n",
       "\n",
       "   protype                                              oploc  regcap  reccap  \\\n",
       "0      nan                                   2367b4cac96d8598   50.00     nan   \n",
       "1      nan  31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...   10.00     nan   \n",
       "2      nan                                   2367b4cac96d8598  100.00     nan   \n",
       "3      nan                                   2367b4cac96d8598   10.00     nan   \n",
       "4      nan                                   2367b4cac96d8598  100.00     nan   \n",
       "\n",
       "   forreccap  forregcap  congro  enttypegb  \n",
       "0        nan        nan     nan       1151  \n",
       "1        nan        nan     nan       9600  \n",
       "2        nan        nan     nan       1151  \n",
       "3        nan        nan     nan       4540  \n",
       "4        nan        nan     nan       1130  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据集1：base_info.csv\n",
    "# 包含数据集7和8中涉及到的所有企业的基本信息，每一行代表一个企业的基本数据，每一行有33列，其中id列为企业唯一标识，列之间采用“,”分隔符分割。\n",
    "# 数据格式如下：\n",
    "# [id:企业唯一标识, oplocdistrict:行政区划代码, industryphy:行业类别代码, industryco:行业细类代码, dom:经营地址, opscope:经营范围, enttype:企业类型, enttypeitem:企业类型小类, opfrom:经营期限起, opto:经营期限止, state:状态, orgid:机构标识, jobid:职位标识, adbusign:是否广告经营, townsign:是否城镇, regtype:主题登记类型, empnum:从业人数, compform:组织形式, parnum:合伙人数, exenum:执行人数, opform:经营方式, ptbusscope:兼营范围, venind:风险行业, enttypeminu:企业类型细类, midpreindcode:中西部优势产业代码, protype:项目类型, oploc:经营场所, regcap:注册资本（金）, reccap:实缴资本, forreccap:实缴资本（外方）, forregcap:注册资本（外方）, congro:投资总额, enttypegb:企业（机构）类型]\n",
    "\n",
    "# 基础信息表\n",
    "\n",
    "base_info = pd.read_csv('raw_data/train/base_info.csv')\n",
    "\n",
    "print(base_info.shape)\n",
    "base_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "03972b9a909845518a2150813e13e5c7",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=33.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "id 24865\n",
      "oplocdistrict 16\n",
      "industryphy 20\n",
      "industryco 346\n",
      "dom 23278\n",
      "opscope 20815\n",
      "enttype 17\n",
      "enttypeitem 32\n",
      "opfrom 6620\n",
      "opto 5747\n",
      "state 6\n",
      "orgid 78\n",
      "jobid 434\n",
      "adbusign 2\n",
      "townsign 2\n",
      "regtype 3\n",
      "empnum 63\n",
      "compform 3\n",
      "parnum 52\n",
      "exenum 51\n",
      "opform 34\n",
      "ptbusscope 1\n",
      "venind 4\n",
      "enttypeminu 27\n",
      "midpreindcode 1\n",
      "protype 3\n",
      "oploc 5351\n",
      "regcap 1144\n",
      "reccap 598\n",
      "forreccap 12\n",
      "forregcap 39\n",
      "congro 34\n",
      "enttypegb 53\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for col in tqdm(base_info.columns):\n",
    "    print(col, base_info[col].nunique(dropna=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去掉只有一种类别的 columns\n",
    "\n",
    "del base_info['ptbusscope']\n",
    "del base_info['midpreindcode']\n",
    "\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 去掉类别太多的 columns\n",
    "\n",
    "del base_info['dom']\n",
    "del base_info['opscope']  # 后面可以分词后用 svd 抽取特征, 但感觉效果不大\n",
    "\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>industryco</th>\n",
       "      <th>enttype</th>\n",
       "      <th>enttypeitem</th>\n",
       "      <th>opfrom</th>\n",
       "      <th>opto</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>empnum</th>\n",
       "      <th>compform</th>\n",
       "      <th>parnum</th>\n",
       "      <th>exenum</th>\n",
       "      <th>opform</th>\n",
       "      <th>venind</th>\n",
       "      <th>enttypeminu</th>\n",
       "      <th>protype</th>\n",
       "      <th>oploc</th>\n",
       "      <th>regcap</th>\n",
       "      <th>reccap</th>\n",
       "      <th>forreccap</th>\n",
       "      <th>forregcap</th>\n",
       "      <th>congro</th>\n",
       "      <th>enttypegb</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>340223</td>\n",
       "      <td>M</td>\n",
       "      <td>7513.00</td>\n",
       "      <td>1100</td>\n",
       "      <td>1150.00</td>\n",
       "      <td>2019-07-11 00:00:00</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340223010010000000</td>\n",
       "      <td>340200000000115392</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>50.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3</td>\n",
       "      <td>340222</td>\n",
       "      <td>O</td>\n",
       "      <td>8090.00</td>\n",
       "      <td>9600</td>\n",
       "      <td>nan</td>\n",
       "      <td>2017-09-06</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340222060010000000</td>\n",
       "      <td>340200000000112114</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>10</td>\n",
       "      <td>3.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...</td>\n",
       "      <td>10.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>9600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>9053.00</td>\n",
       "      <td>1100</td>\n",
       "      <td>1150.00</td>\n",
       "      <td>2020-09-14 14:46:30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340202010010000000</td>\n",
       "      <td>400000000000753910</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>1.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>100.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>340221</td>\n",
       "      <td>L</td>\n",
       "      <td>7212.00</td>\n",
       "      <td>4500</td>\n",
       "      <td>4540.00</td>\n",
       "      <td>2015-09-30</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6</td>\n",
       "      <td>340221010010000000</td>\n",
       "      <td>400000000000013538</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>01-以个人财产出资</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>10.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>4540</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>8810.00</td>\n",
       "      <td>1100</td>\n",
       "      <td>1130.00</td>\n",
       "      <td>2017-12-01</td>\n",
       "      <td>2067-11-30</td>\n",
       "      <td>7</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>400000000000283237</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>100.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1130</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  oplocdistrict  \\\n",
       "0  47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4         340223   \n",
       "1  9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3         340222   \n",
       "2  59b38c56de3836838082cfcb1a298951abfe15e6940c49ba         340202   \n",
       "3  e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818         340221   \n",
       "4  f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb         340202   \n",
       "\n",
       "  industryphy  industryco  enttype  enttypeitem               opfrom  \\\n",
       "0           M     7513.00     1100      1150.00  2019-07-11 00:00:00   \n",
       "1           O     8090.00     9600          nan           2017-09-06   \n",
       "2           R     9053.00     1100      1150.00  2020-09-14 14:46:30   \n",
       "3           L     7212.00     4500      4540.00           2015-09-30   \n",
       "4           R     8810.00     1100      1130.00           2017-12-01   \n",
       "\n",
       "         opto  state               orgid               jobid  adbusign  \\\n",
       "0         NaN      6  340223010010000000  340200000000115392         0   \n",
       "1         NaN      6  340222060010000000  340200000000112114         0   \n",
       "2         NaN      6  340202010010000000  400000000000753910         0   \n",
       "3         NaN      6  340221010010000000  400000000000013538         0   \n",
       "4  2067-11-30      7  340200000000000000  400000000000283237         0   \n",
       "\n",
       "   townsign  regtype  empnum  compform  parnum  exenum      opform  venind  \\\n",
       "0         0        1    5.00       nan     nan     nan         NaN     nan   \n",
       "1         1        1    3.00      1.00     nan     nan          10    3.00   \n",
       "2         0        1    2.00       nan    1.00     nan         NaN     nan   \n",
       "3         1        1    2.00       nan     nan     nan  01-以个人财产出资     nan   \n",
       "4         0        1     nan       nan     nan     nan         NaN     nan   \n",
       "\n",
       "   enttypeminu  protype                                              oploc  \\\n",
       "0      1151.00      nan                                   2367b4cac96d8598   \n",
       "1          nan      nan  31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...   \n",
       "2      1151.00      nan                                   2367b4cac96d8598   \n",
       "3          nan      nan                                   2367b4cac96d8598   \n",
       "4          nan      nan                                   2367b4cac96d8598   \n",
       "\n",
       "   regcap  reccap  forreccap  forregcap  congro  enttypegb  \n",
       "0   50.00     nan        nan        nan     nan       1151  \n",
       "1   10.00     nan        nan        nan     nan       9600  \n",
       "2  100.00     nan        nan        nan     nan       1151  \n",
       "3   10.00     nan        nan        nan     nan       4540  \n",
       "4  100.00     nan        nan        nan     nan       1130  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "base_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "22"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 时间转换, 暂时先抽取年份特征\n",
    "\n",
    "base_info['opfrom'] = pd.to_datetime(base_info.opfrom)\n",
    "base_info['opfrom_year'] = base_info['opfrom'].dt.year.astype('int')\n",
    "\n",
    "base_info['opto'] = pd.to_datetime(base_info.opto)\n",
    "base_info['opto_year'] = base_info['opto'].dt.year.fillna(-1).astype('int')\n",
    "\n",
    "del base_info['opfrom']\n",
    "del base_info['opto']\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>industryco</th>\n",
       "      <th>enttype</th>\n",
       "      <th>enttypeitem</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>empnum</th>\n",
       "      <th>compform</th>\n",
       "      <th>parnum</th>\n",
       "      <th>exenum</th>\n",
       "      <th>opform</th>\n",
       "      <th>venind</th>\n",
       "      <th>enttypeminu</th>\n",
       "      <th>protype</th>\n",
       "      <th>oploc</th>\n",
       "      <th>regcap</th>\n",
       "      <th>reccap</th>\n",
       "      <th>forreccap</th>\n",
       "      <th>forregcap</th>\n",
       "      <th>congro</th>\n",
       "      <th>enttypegb</th>\n",
       "      <th>opfrom_year</th>\n",
       "      <th>opto_year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>340223</td>\n",
       "      <td>M</td>\n",
       "      <td>7513.00</td>\n",
       "      <td>1100</td>\n",
       "      <td>1150.00</td>\n",
       "      <td>6</td>\n",
       "      <td>340223010010000000</td>\n",
       "      <td>340200000000115392</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>50.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151</td>\n",
       "      <td>2019</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3</td>\n",
       "      <td>340222</td>\n",
       "      <td>O</td>\n",
       "      <td>8090.00</td>\n",
       "      <td>9600</td>\n",
       "      <td>nan</td>\n",
       "      <td>6</td>\n",
       "      <td>340222060010000000</td>\n",
       "      <td>340200000000112114</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>10</td>\n",
       "      <td>3.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...</td>\n",
       "      <td>10.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>9600</td>\n",
       "      <td>2017</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>9053.00</td>\n",
       "      <td>1100</td>\n",
       "      <td>1150.00</td>\n",
       "      <td>6</td>\n",
       "      <td>340202010010000000</td>\n",
       "      <td>400000000000753910</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>1.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>100.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1151</td>\n",
       "      <td>2020</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>340221</td>\n",
       "      <td>L</td>\n",
       "      <td>7212.00</td>\n",
       "      <td>4500</td>\n",
       "      <td>4540.00</td>\n",
       "      <td>6</td>\n",
       "      <td>340221010010000000</td>\n",
       "      <td>400000000000013538</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>01-以个人财产出资</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>10.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>4540</td>\n",
       "      <td>2015</td>\n",
       "      <td>-1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>340202</td>\n",
       "      <td>R</td>\n",
       "      <td>8810.00</td>\n",
       "      <td>1100</td>\n",
       "      <td>1130.00</td>\n",
       "      <td>7</td>\n",
       "      <td>340200000000000000</td>\n",
       "      <td>400000000000283237</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>NaN</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "      <td>100.00</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>nan</td>\n",
       "      <td>1130</td>\n",
       "      <td>2017</td>\n",
       "      <td>2067</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  oplocdistrict  \\\n",
       "0  47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4         340223   \n",
       "1  9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3         340222   \n",
       "2  59b38c56de3836838082cfcb1a298951abfe15e6940c49ba         340202   \n",
       "3  e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818         340221   \n",
       "4  f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb         340202   \n",
       "\n",
       "  industryphy  industryco  enttype  enttypeitem  state               orgid  \\\n",
       "0           M     7513.00     1100      1150.00      6  340223010010000000   \n",
       "1           O     8090.00     9600          nan      6  340222060010000000   \n",
       "2           R     9053.00     1100      1150.00      6  340202010010000000   \n",
       "3           L     7212.00     4500      4540.00      6  340221010010000000   \n",
       "4           R     8810.00     1100      1130.00      7  340200000000000000   \n",
       "\n",
       "                jobid  adbusign  townsign  regtype  empnum  compform  parnum  \\\n",
       "0  340200000000115392         0         0        1    5.00       nan     nan   \n",
       "1  340200000000112114         0         1        1    3.00      1.00     nan   \n",
       "2  400000000000753910         0         0        1    2.00       nan    1.00   \n",
       "3  400000000000013538         0         1        1    2.00       nan     nan   \n",
       "4  400000000000283237         0         0        1     nan       nan     nan   \n",
       "\n",
       "   exenum      opform  venind  enttypeminu  protype  \\\n",
       "0     nan         NaN     nan      1151.00      nan   \n",
       "1     nan          10    3.00          nan      nan   \n",
       "2     nan         NaN     nan      1151.00      nan   \n",
       "3     nan  01-以个人财产出资     nan          nan      nan   \n",
       "4     nan         NaN     nan          nan      nan   \n",
       "\n",
       "                                               oploc  regcap  reccap  \\\n",
       "0                                   2367b4cac96d8598   50.00     nan   \n",
       "1  31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...   10.00     nan   \n",
       "2                                   2367b4cac96d8598  100.00     nan   \n",
       "3                                   2367b4cac96d8598   10.00     nan   \n",
       "4                                   2367b4cac96d8598  100.00     nan   \n",
       "\n",
       "   forreccap  forregcap  congro  enttypegb  opfrom_year  opto_year  \n",
       "0        nan        nan     nan       1151         2019         -1  \n",
       "1        nan        nan     nan       9600         2017         -1  \n",
       "2        nan        nan     nan       1151         2020         -1  \n",
       "3        nan        nan     nan       4540         2015         -1  \n",
       "4        nan        nan     nan       1130         2017       2067  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "base_info.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "id                object\n",
       "oplocdistrict      int64\n",
       "industryphy       object\n",
       "industryco       float64\n",
       "enttype            int64\n",
       "enttypeitem      float64\n",
       "state              int64\n",
       "orgid              int64\n",
       "jobid              int64\n",
       "adbusign           int64\n",
       "townsign           int64\n",
       "regtype            int64\n",
       "empnum           float64\n",
       "compform         float64\n",
       "parnum           float64\n",
       "exenum           float64\n",
       "opform            object\n",
       "venind           float64\n",
       "enttypeminu      float64\n",
       "protype          float64\n",
       "oploc             object\n",
       "regcap           float64\n",
       "reccap           float64\n",
       "forreccap        float64\n",
       "forregcap        float64\n",
       "congro           float64\n",
       "enttypegb          int64\n",
       "opfrom_year        int64\n",
       "opto_year          int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "base_info.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a0e6c92ce73e40dbac9c9ef9f26e1e33",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=6.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# 类别清理\n",
    "\n",
    "for col in tqdm(['industryco', 'enttypeitem', 'compform', 'venind', \n",
    "                 'enttypeminu', 'protype']):\n",
    "    base_info[col] = base_info[col].fillna(-1).astype('int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>empnum</td>\n",
       "      <td>19615.00</td>\n",
       "      <td>4.40</td>\n",
       "      <td>15.39</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>5.00</td>\n",
       "      <td>1500.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>parnum</td>\n",
       "      <td>2339.00</td>\n",
       "      <td>4.59</td>\n",
       "      <td>8.75</td>\n",
       "      <td>0.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>2.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>100.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>exenum</td>\n",
       "      <td>1378.00</td>\n",
       "      <td>77.75</td>\n",
       "      <td>2693.74</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>100000.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>regcap</td>\n",
       "      <td>24674.00</td>\n",
       "      <td>5151.44</td>\n",
       "      <td>67770.86</td>\n",
       "      <td>0.00</td>\n",
       "      <td>15.00</td>\n",
       "      <td>80.00</td>\n",
       "      <td>500.00</td>\n",
       "      <td>5000100.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>reccap</td>\n",
       "      <td>7084.00</td>\n",
       "      <td>4198.17</td>\n",
       "      <td>36537.98</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>100.00</td>\n",
       "      <td>1278900.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>forreccap</td>\n",
       "      <td>227.00</td>\n",
       "      <td>292.64</td>\n",
       "      <td>1841.31</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>15428.17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>forregcap</td>\n",
       "      <td>250.00</td>\n",
       "      <td>1212.58</td>\n",
       "      <td>8113.27</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>88817.92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>congro</td>\n",
       "      <td>249.00</td>\n",
       "      <td>2805.26</td>\n",
       "      <td>18131.95</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>221453.76</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             count    mean      std  min   25%   50%    75%        max\n",
       "empnum    19615.00    4.40    15.39 0.00  2.00  3.00   5.00    1500.00\n",
       "parnum     2339.00    4.59     8.75 0.00  2.00  2.00   3.00     100.00\n",
       "exenum     1378.00   77.75  2693.74 0.00  1.00  1.00   3.00  100000.00\n",
       "regcap    24674.00 5151.44 67770.86 0.00 15.00 80.00 500.00 5000100.00\n",
       "reccap     7084.00 4198.17 36537.98 0.00  0.00  0.00 100.00 1278900.00\n",
       "forreccap   227.00  292.64  1841.31 0.00  0.00  0.00   0.00   15428.17\n",
       "forregcap   250.00 1212.58  8113.27 0.00  0.00  0.00   0.00   88817.92\n",
       "congro      249.00 2805.26 18131.95 0.00  0.00  0.00   0.00  221453.76"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数字类型\n",
    "\n",
    "base_info.select_dtypes(['float64']).describe().T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "31dd366303254508abb34bbadfcfd5db",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=8.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# 数字类型缺失值太多, 且部分 columns std 较大, 先考虑用中值填充\n",
    "\n",
    "for col in tqdm(base_info.select_dtypes(['float64']).columns):\n",
    "    base_info[col] = base_info[col].fillna(base_info[col].median())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>opform</th>\n",
       "      <th>oploc</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>M</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3</td>\n",
       "      <td>O</td>\n",
       "      <td>10</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>R</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>L</td>\n",
       "      <td>01-以个人财产出资</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>R</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24860</td>\n",
       "      <td>f1c1045b13d18329a2bd99d2a7e2227688c0d69bf1d1e325</td>\n",
       "      <td>O</td>\n",
       "      <td>10</td>\n",
       "      <td>f67c1b92f52ac52e424308ab51241cdef9be3e39c8e1c6...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24861</td>\n",
       "      <td>f000950527a6feb6bde38216d7cbbf32e66d3a3a96d4dbda</td>\n",
       "      <td>J</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24862</td>\n",
       "      <td>da8691b210adb3f65b43370d3a362f4aa1d3b16b5ba0c9d7</td>\n",
       "      <td>O</td>\n",
       "      <td>10</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24863</td>\n",
       "      <td>516ab81418ed215dcbbf0614a7b929e691f8eed153d7bb31</td>\n",
       "      <td>O</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2367b4cac96d8598</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24864</td>\n",
       "      <td>9c7fa510616a68303d3427d4bfd4b0cf3e4843f2bf3f637a</td>\n",
       "      <td>N</td>\n",
       "      <td>10</td>\n",
       "      <td>31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>24865 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                     id industryphy  \\\n",
       "0      47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4           M   \n",
       "1      9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3           O   \n",
       "2      59b38c56de3836838082cfcb1a298951abfe15e6940c49ba           R   \n",
       "3      e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818           L   \n",
       "4      f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb           R   \n",
       "...                                                 ...         ...   \n",
       "24860  f1c1045b13d18329a2bd99d2a7e2227688c0d69bf1d1e325           O   \n",
       "24861  f000950527a6feb6bde38216d7cbbf32e66d3a3a96d4dbda           J   \n",
       "24862  da8691b210adb3f65b43370d3a362f4aa1d3b16b5ba0c9d7           O   \n",
       "24863  516ab81418ed215dcbbf0614a7b929e691f8eed153d7bb31           O   \n",
       "24864  9c7fa510616a68303d3427d4bfd4b0cf3e4843f2bf3f637a           N   \n",
       "\n",
       "           opform                                              oploc  \n",
       "0             NaN                                   2367b4cac96d8598  \n",
       "1              10  31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...  \n",
       "2             NaN                                   2367b4cac96d8598  \n",
       "3      01-以个人财产出资                                   2367b4cac96d8598  \n",
       "4             NaN                                   2367b4cac96d8598  \n",
       "...           ...                                                ...  \n",
       "24860          10  f67c1b92f52ac52e424308ab51241cdef9be3e39c8e1c6...  \n",
       "24861         NaN                                   2367b4cac96d8598  \n",
       "24862          10                                   2367b4cac96d8598  \n",
       "24863         NaN                                   2367b4cac96d8598  \n",
       "24864          10  31487d8f256f16bd6244b7251be2ebb27b17bdfd95c8f3...  \n",
       "\n",
       "[24865 rows x 4 columns]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# object 类型\n",
    "\n",
    "base_info.select_dtypes(['object'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据清理\n",
    "\n",
    "base_info['opform'] = base_info['opform'].replace('01', '01-以个人财产出资').replace('02', '02-以家庭共有财产作为个人出资')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2ac4b21381f64e628811422b09d88fb8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=10.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# 数据比较长尾, label encoding 和 freq 处理\n",
    "\n",
    "for col in tqdm(['industryphy', 'opform', 'oploc', 'orgid', 'jobid', 'oplocdistrict',\n",
    "                 'enttypegb', 'industryco', 'enttype', 'enttypeitem']):\n",
    "    lbl = LabelEncoder()\n",
    "    base_info[col] = lbl.fit_transform(base_info[col].astype(str))\n",
    "    vc = base_info[col].value_counts(dropna=True, normalize=True).to_dict()\n",
    "    base_info[f'{col}_freq'] = base_info[col].map(vc)\n",
    "#     base_info[col] = base_info[col].map(vc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "24"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过上一次模型的重要度输出发现的分裂次数为 0 的特征\n",
    "\n",
    "del base_info['forreccap']\n",
    "del base_info['forregcap']\n",
    "del base_info['protype']\n",
    "del base_info['congro']\n",
    "\n",
    "gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 24865 entries, 0 to 24864\n",
      "Data columns (total 35 columns):\n",
      "id                    24865 non-null object\n",
      "oplocdistrict         24865 non-null int64\n",
      "industryphy           24865 non-null int64\n",
      "industryco            24865 non-null int64\n",
      "enttype               24865 non-null int64\n",
      "enttypeitem           24865 non-null int64\n",
      "state                 24865 non-null int64\n",
      "orgid                 24865 non-null int64\n",
      "jobid                 24865 non-null int64\n",
      "adbusign              24865 non-null int64\n",
      "townsign              24865 non-null int64\n",
      "regtype               24865 non-null int64\n",
      "empnum                24865 non-null float64\n",
      "compform              24865 non-null int64\n",
      "parnum                24865 non-null float64\n",
      "exenum                24865 non-null float64\n",
      "opform                24865 non-null int64\n",
      "venind                24865 non-null int64\n",
      "enttypeminu           24865 non-null int64\n",
      "oploc                 24865 non-null int64\n",
      "regcap                24865 non-null float64\n",
      "reccap                24865 non-null float64\n",
      "enttypegb             24865 non-null int64\n",
      "opfrom_year           24865 non-null int64\n",
      "opto_year             24865 non-null int64\n",
      "industryphy_freq      24865 non-null float64\n",
      "opform_freq           24865 non-null float64\n",
      "oploc_freq            24865 non-null float64\n",
      "orgid_freq            24865 non-null float64\n",
      "jobid_freq            24865 non-null float64\n",
      "oplocdistrict_freq    24865 non-null float64\n",
      "enttypegb_freq        24865 non-null float64\n",
      "industryco_freq       24865 non-null float64\n",
      "enttype_freq          24865 non-null float64\n",
      "enttypeitem_freq      24865 non-null float64\n",
      "dtypes: float64(15), int64(19), object(1)\n",
      "memory usage: 6.6+ MB\n"
     ]
    }
   ],
   "source": [
    "base_info.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(24865, 36)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>industryco</th>\n",
       "      <th>enttype</th>\n",
       "      <th>enttypeitem</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>empnum</th>\n",
       "      <th>compform</th>\n",
       "      <th>parnum</th>\n",
       "      <th>exenum</th>\n",
       "      <th>opform</th>\n",
       "      <th>venind</th>\n",
       "      <th>enttypeminu</th>\n",
       "      <th>oploc</th>\n",
       "      <th>regcap</th>\n",
       "      <th>reccap</th>\n",
       "      <th>enttypegb</th>\n",
       "      <th>opfrom_year</th>\n",
       "      <th>opto_year</th>\n",
       "      <th>industryphy_freq</th>\n",
       "      <th>opform_freq</th>\n",
       "      <th>oploc_freq</th>\n",
       "      <th>orgid_freq</th>\n",
       "      <th>jobid_freq</th>\n",
       "      <th>oplocdistrict_freq</th>\n",
       "      <th>enttypegb_freq</th>\n",
       "      <th>industryco_freq</th>\n",
       "      <th>enttype_freq</th>\n",
       "      <th>enttypeitem_freq</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>198</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>62</td>\n",
       "      <td>112</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>1151</td>\n",
       "      <td>108</td>\n",
       "      <td>50.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>6</td>\n",
       "      <td>2019</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.28</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.23</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "      <td>244</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>58</td>\n",
       "      <td>61</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3.00</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>1989</td>\n",
       "      <td>10.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>52</td>\n",
       "      <td>2017</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.41</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.02</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.33</td>\n",
       "      <td>nan</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "      <td>337</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>32</td>\n",
       "      <td>422</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>1151</td>\n",
       "      <td>108</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>6</td>\n",
       "      <td>2020</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.15</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.23</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "      <td>140</td>\n",
       "      <td>9</td>\n",
       "      <td>19</td>\n",
       "      <td>6</td>\n",
       "      <td>50</td>\n",
       "      <td>321</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>108</td>\n",
       "      <td>10.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>39</td>\n",
       "      <td>2015</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.08</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "      <td>312</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>346</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>108</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4</td>\n",
       "      <td>2017</td>\n",
       "      <td>2067</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  oplocdistrict  \\\n",
       "0  47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4             10   \n",
       "1  9c7fa510616a683058ce97d0bc768a621cd85ab1e87da2a3              9   \n",
       "2  59b38c56de3836838082cfcb1a298951abfe15e6940c49ba              3   \n",
       "3  e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818              8   \n",
       "4  f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb              3   \n",
       "\n",
       "   industryphy  industryco  enttype  enttypeitem  state  orgid  jobid  \\\n",
       "0           12         198        0            5      6     62    112   \n",
       "1           14         244       16            0      6     58     61   \n",
       "2           17         337        0            5      6     32    422   \n",
       "3           11         140        9           19      6     50    321   \n",
       "4           17         312        0            3      7      1    346   \n",
       "\n",
       "   adbusign  townsign  regtype  empnum  compform  parnum  exenum  opform  \\\n",
       "0         0         0        1    5.00        -1    2.00    1.00       4   \n",
       "1         0         1        1    3.00         1    2.00    1.00       3   \n",
       "2         0         0        1    2.00        -1    1.00    1.00       4   \n",
       "3         0         1        1    2.00        -1    2.00    1.00       1   \n",
       "4         0         0        1    3.00        -1    2.00    1.00       4   \n",
       "\n",
       "   venind  enttypeminu  oploc  regcap  reccap  enttypegb  opfrom_year  \\\n",
       "0      -1         1151    108   50.00    0.00          6         2019   \n",
       "1       3           -1   1989   10.00    0.00         52         2017   \n",
       "2      -1         1151    108  100.00    0.00          6         2020   \n",
       "3      -1           -1    108   10.00    0.00         39         2015   \n",
       "4      -1           -1    108  100.00    0.00          4         2017   \n",
       "\n",
       "   opto_year  industryphy_freq  opform_freq  oploc_freq  orgid_freq  \\\n",
       "0         -1              0.28         0.64        0.76        0.04   \n",
       "1         -1              0.41         0.33        0.01        0.02   \n",
       "2         -1              0.12         0.64        0.76        0.15   \n",
       "3         -1              0.06         0.03        0.76        0.05   \n",
       "4       2067              0.12         0.64        0.76        0.09   \n",
       "\n",
       "   jobid_freq  oplocdistrict_freq  enttypegb_freq  industryco_freq  \\\n",
       "0        0.01                0.07            0.19             0.01   \n",
       "1        0.01                0.05            0.33             0.12   \n",
       "2        0.01                0.22            0.19             0.00   \n",
       "3        0.00                0.08            0.03             0.03   \n",
       "4        0.00                0.22            0.30             0.01   \n",
       "\n",
       "   enttype_freq  enttypeitem_freq  label  \n",
       "0          0.57              0.23   0.00  \n",
       "1          0.33              0.33    nan  \n",
       "2          0.57              0.23   0.00  \n",
       "3          0.09              0.03   0.00  \n",
       "4          0.57              0.30   0.00  "
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 划分训练集和测试集\n",
    "\n",
    "entprise_evaluate.columns = ['id', 'label']\n",
    "\n",
    "labels = pd.concat([entprise_info, entprise_evaluate])\n",
    "df = pd.merge(base_info, labels, on='id', how='left')\n",
    "\n",
    "print(df.shape)\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 缺失值填充\n",
    "\n",
    "df[[col for col in df.columns if col != 'label']].fillna(-1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(14865, 36) (10000, 36)\n"
     ]
    }
   ],
   "source": [
    "train = df[df.label.notna()]\n",
    "test = df[df.label.isna()]\n",
    "\n",
    "print(train.shape, test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>oplocdistrict</th>\n",
       "      <th>industryphy</th>\n",
       "      <th>industryco</th>\n",
       "      <th>enttype</th>\n",
       "      <th>enttypeitem</th>\n",
       "      <th>state</th>\n",
       "      <th>orgid</th>\n",
       "      <th>jobid</th>\n",
       "      <th>adbusign</th>\n",
       "      <th>townsign</th>\n",
       "      <th>regtype</th>\n",
       "      <th>empnum</th>\n",
       "      <th>compform</th>\n",
       "      <th>parnum</th>\n",
       "      <th>exenum</th>\n",
       "      <th>opform</th>\n",
       "      <th>venind</th>\n",
       "      <th>enttypeminu</th>\n",
       "      <th>oploc</th>\n",
       "      <th>regcap</th>\n",
       "      <th>reccap</th>\n",
       "      <th>enttypegb</th>\n",
       "      <th>opfrom_year</th>\n",
       "      <th>opto_year</th>\n",
       "      <th>industryphy_freq</th>\n",
       "      <th>opform_freq</th>\n",
       "      <th>oploc_freq</th>\n",
       "      <th>orgid_freq</th>\n",
       "      <th>jobid_freq</th>\n",
       "      <th>oplocdistrict_freq</th>\n",
       "      <th>enttypegb_freq</th>\n",
       "      <th>industryco_freq</th>\n",
       "      <th>enttype_freq</th>\n",
       "      <th>enttypeitem_freq</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4</td>\n",
       "      <td>10</td>\n",
       "      <td>12</td>\n",
       "      <td>198</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>62</td>\n",
       "      <td>112</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>1151</td>\n",
       "      <td>108</td>\n",
       "      <td>50.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>6</td>\n",
       "      <td>2019</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.28</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.04</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.07</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.23</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>59b38c56de3836838082cfcb1a298951abfe15e6940c49ba</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "      <td>337</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>32</td>\n",
       "      <td>422</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>1151</td>\n",
       "      <td>108</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>6</td>\n",
       "      <td>2020</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.15</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.19</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.23</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818</td>\n",
       "      <td>8</td>\n",
       "      <td>11</td>\n",
       "      <td>140</td>\n",
       "      <td>9</td>\n",
       "      <td>19</td>\n",
       "      <td>6</td>\n",
       "      <td>50</td>\n",
       "      <td>321</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>1</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>108</td>\n",
       "      <td>10.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>39</td>\n",
       "      <td>2015</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.06</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.08</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.03</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb</td>\n",
       "      <td>3</td>\n",
       "      <td>17</td>\n",
       "      <td>312</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>346</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3.00</td>\n",
       "      <td>-1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>4</td>\n",
       "      <td>-1</td>\n",
       "      <td>-1</td>\n",
       "      <td>108</td>\n",
       "      <td>100.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>4</td>\n",
       "      <td>2017</td>\n",
       "      <td>2067</td>\n",
       "      <td>0.12</td>\n",
       "      <td>0.64</td>\n",
       "      <td>0.76</td>\n",
       "      <td>0.09</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.22</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.57</td>\n",
       "      <td>0.30</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>9c7fa510616a6830b878f3c8c4317d93e1b022e7f22ae231</td>\n",
       "      <td>9</td>\n",
       "      <td>14</td>\n",
       "      <td>239</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>60</td>\n",
       "      <td>37</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>5.00</td>\n",
       "      <td>1</td>\n",
       "      <td>2.00</td>\n",
       "      <td>1.00</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>-1</td>\n",
       "      <td>3960</td>\n",
       "      <td>20.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>52</td>\n",
       "      <td>2020</td>\n",
       "      <td>-1</td>\n",
       "      <td>0.41</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.01</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.33</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  oplocdistrict  \\\n",
       "0  47645761dc56bb8c5fae00114b768b5d9b6e917c3aec07c4             10   \n",
       "2  59b38c56de3836838082cfcb1a298951abfe15e6940c49ba              3   \n",
       "3  e9f7b28ec10e047000d16ab79e1b5e6da434a1697cce7818              8   \n",
       "4  f000950527a6feb63ee1ce82bb22ddd1ab8b8fdffa3b91fb              3   \n",
       "6  9c7fa510616a6830b878f3c8c4317d93e1b022e7f22ae231              9   \n",
       "\n",
       "   industryphy  industryco  enttype  enttypeitem  state  orgid  jobid  \\\n",
       "0           12         198        0            5      6     62    112   \n",
       "2           17         337        0            5      6     32    422   \n",
       "3           11         140        9           19      6     50    321   \n",
       "4           17         312        0            3      7      1    346   \n",
       "6           14         239       16            0      6     60     37   \n",
       "\n",
       "   adbusign  townsign  regtype  empnum  compform  parnum  exenum  opform  \\\n",
       "0         0         0        1    5.00        -1    2.00    1.00       4   \n",
       "2         0         0        1    2.00        -1    1.00    1.00       4   \n",
       "3         0         1        1    2.00        -1    2.00    1.00       1   \n",
       "4         0         0        1    3.00        -1    2.00    1.00       4   \n",
       "6         0         1        1    5.00         1    2.00    1.00       3   \n",
       "\n",
       "   venind  enttypeminu  oploc  regcap  reccap  enttypegb  opfrom_year  \\\n",
       "0      -1         1151    108   50.00    0.00          6         2019   \n",
       "2      -1         1151    108  100.00    0.00          6         2020   \n",
       "3      -1           -1    108   10.00    0.00         39         2015   \n",
       "4      -1           -1    108  100.00    0.00          4         2017   \n",
       "6       3           -1   3960   20.00    0.00         52         2020   \n",
       "\n",
       "   opto_year  industryphy_freq  opform_freq  oploc_freq  orgid_freq  \\\n",
       "0         -1              0.28         0.64        0.76        0.04   \n",
       "2         -1              0.12         0.64        0.76        0.15   \n",
       "3         -1              0.06         0.03        0.76        0.05   \n",
       "4       2067              0.12         0.64        0.76        0.09   \n",
       "6         -1              0.41         0.33        0.00        0.00   \n",
       "\n",
       "   jobid_freq  oplocdistrict_freq  enttypegb_freq  industryco_freq  \\\n",
       "0        0.01                0.07            0.19             0.01   \n",
       "2        0.01                0.22            0.19             0.00   \n",
       "3        0.00                0.08            0.03             0.03   \n",
       "4        0.00                0.22            0.30             0.01   \n",
       "6        0.00                0.05            0.33             0.01   \n",
       "\n",
       "   enttype_freq  enttypeitem_freq  label  \n",
       "0          0.57              0.23   0.00  \n",
       "2          0.57              0.23   0.00  \n",
       "3          0.09              0.03   0.00  \n",
       "4          0.57              0.30   0.00  \n",
       "6          0.33              0.33   0.00  "
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Fold_1 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[36]\ttrain's binary_logloss: 0.0961808\ttrain's f1: 0.843458\tvalid's binary_logloss: 0.0960928\tvalid's f1: 0.855721\n",
      "\n",
      "Fold_2 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[34]\ttrain's binary_logloss: 0.0982822\ttrain's f1: 0.838471\tvalid's binary_logloss: 0.102158\tvalid's f1: 0.830918\n",
      "\n",
      "Fold_3 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[38]\ttrain's binary_logloss: 0.0933097\ttrain's f1: 0.850746\tvalid's binary_logloss: 0.0970162\tvalid's f1: 0.826966\n",
      "\n",
      "Fold_4 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[33]\ttrain's binary_logloss: 0.0989391\ttrain's f1: 0.825377\tvalid's binary_logloss: 0.102725\tvalid's f1: 0.826733\n",
      "\n",
      "Fold_5 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 50 rounds\n",
      "Early stopping, best iteration is:\n",
      "[37]\ttrain's binary_logloss: 0.0947528\ttrain's f1: 0.843537\tvalid's binary_logloss: 0.0964571\tvalid's f1: 0.837529\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column</th>\n",
       "      <th>importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>industryco</td>\n",
       "      <td>89.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>regcap</td>\n",
       "      <td>62.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>jobid_freq</td>\n",
       "      <td>47.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>enttypegb_freq</td>\n",
       "      <td>45.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>enttypegb</td>\n",
       "      <td>40.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>reccap</td>\n",
       "      <td>40.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>opto_year</td>\n",
       "      <td>36.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>jobid</td>\n",
       "      <td>35.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>opfrom_year</td>\n",
       "      <td>35.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>enttypeitem</td>\n",
       "      <td>33.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>industryphy_freq</td>\n",
       "      <td>32.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>11</td>\n",
       "      <td>industryphy</td>\n",
       "      <td>31.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>12</td>\n",
       "      <td>enttype_freq</td>\n",
       "      <td>28.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>13</td>\n",
       "      <td>industryco_freq</td>\n",
       "      <td>25.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>14</td>\n",
       "      <td>empnum</td>\n",
       "      <td>23.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>15</td>\n",
       "      <td>enttype</td>\n",
       "      <td>19.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>16</td>\n",
       "      <td>enttypeitem_freq</td>\n",
       "      <td>12.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>17</td>\n",
       "      <td>orgid</td>\n",
       "      <td>12.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>18</td>\n",
       "      <td>orgid_freq</td>\n",
       "      <td>10.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>19</td>\n",
       "      <td>state</td>\n",
       "      <td>9.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>enttypeminu</td>\n",
       "      <td>9.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>21</td>\n",
       "      <td>oplocdistrict</td>\n",
       "      <td>6.40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>22</td>\n",
       "      <td>oplocdistrict_freq</td>\n",
       "      <td>6.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>23</td>\n",
       "      <td>townsign</td>\n",
       "      <td>3.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>24</td>\n",
       "      <td>regtype</td>\n",
       "      <td>2.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>25</td>\n",
       "      <td>venind</td>\n",
       "      <td>1.60</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>26</td>\n",
       "      <td>opform</td>\n",
       "      <td>0.80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>27</td>\n",
       "      <td>opform_freq</td>\n",
       "      <td>0.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>28</td>\n",
       "      <td>compform</td>\n",
       "      <td>0.20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>29</td>\n",
       "      <td>exenum</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30</td>\n",
       "      <td>oploc</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>31</td>\n",
       "      <td>oploc_freq</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>32</td>\n",
       "      <td>parnum</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>33</td>\n",
       "      <td>adbusign</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                column  importance\n",
       "0           industryco       89.80\n",
       "1               regcap       62.00\n",
       "2           jobid_freq       47.60\n",
       "3       enttypegb_freq       45.80\n",
       "4            enttypegb       40.00\n",
       "5               reccap       40.00\n",
       "6            opto_year       36.00\n",
       "7                jobid       35.40\n",
       "8          opfrom_year       35.20\n",
       "9          enttypeitem       33.20\n",
       "10    industryphy_freq       32.00\n",
       "11         industryphy       31.40\n",
       "12        enttype_freq       28.00\n",
       "13     industryco_freq       25.20\n",
       "14              empnum       23.00\n",
       "15             enttype       19.20\n",
       "16    enttypeitem_freq       12.40\n",
       "17               orgid       12.00\n",
       "18          orgid_freq       10.20\n",
       "19               state        9.60\n",
       "20         enttypeminu        9.20\n",
       "21       oplocdistrict        6.40\n",
       "22  oplocdistrict_freq        6.20\n",
       "23            townsign        3.20\n",
       "24             regtype        2.00\n",
       "25              venind        1.60\n",
       "26              opform        0.80\n",
       "27         opform_freq        0.20\n",
       "28            compform        0.20\n",
       "29              exenum        0.00\n",
       "30               oploc        0.00\n",
       "31          oploc_freq        0.00\n",
       "32              parnum        0.00\n",
       "33            adbusign        0.00"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ycol = 'label'\n",
    "feature_names = list(\n",
    "    filter(lambda x: x not in [ycol, 'id'], train.columns))\n",
    "\n",
    "model = lgb.LGBMClassifier(objective='binary',\n",
    "                           boosting_type='gbdt',\n",
    "                           tree_learner='serial',\n",
    "                           num_leaves=64,\n",
    "                           max_depth=8,\n",
    "                           learning_rate=0.02,\n",
    "                           n_estimators=10000,\n",
    "                           subsample=0.8,\n",
    "                           feature_fraction=0.8,\n",
    "                           reg_alpha=0.3,\n",
    "                           reg_lambda=0.5,\n",
    "                           random_state=2020,\n",
    "                           is_unbalance=True)\n",
    "\n",
    "\n",
    "oof = []\n",
    "prediction = test[['id']]\n",
    "prediction[f'{ycol}_0'] = 0\n",
    "prediction[f'{ycol}_1'] = 0\n",
    "df_importance_list = []\n",
    "\n",
    "def f1_score_custom(y_true, y_pred):\n",
    "    y_pred = y_pred.round()\n",
    "    return 'f1', f1_score(y_true, y_pred), True\n",
    "\n",
    "kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=2020)\n",
    "for fold_id, (trn_idx, val_idx) in enumerate(kfold.split(train[feature_names], train[ycol])):\n",
    "    X_train = train.iloc[trn_idx][feature_names]\n",
    "    Y_train = train.iloc[trn_idx][ycol]\n",
    "\n",
    "    X_val = train.iloc[val_idx][feature_names]\n",
    "    Y_val = train.iloc[val_idx][ycol]\n",
    "\n",
    "    print('\\nFold_{} Training ================================\\n'.format(fold_id+1))\n",
    "    \n",
    "\n",
    "    \n",
    "    lgb_model = model.fit(X_train,\n",
    "                          Y_train,\n",
    "                          eval_names=['train', 'valid'],\n",
    "                          eval_set=[(X_train, Y_train), (X_val, Y_val)],\n",
    "                          verbose=500,\n",
    "                          eval_metric=lambda y_true, y_pred: f1_score_custom(y_true, y_pred),\n",
    "                          early_stopping_rounds=50)\n",
    "\n",
    "    pred_val = lgb_model.predict_proba(\n",
    "        X_val, num_iteration=lgb_model.best_iteration_)\n",
    "    df_oof = train.iloc[val_idx][['id', ycol]].copy()\n",
    "    df_oof[f'{ycol}_0'] = pred_val[:,0]\n",
    "    df_oof[f'{ycol}_1'] = pred_val[:,1]\n",
    "    oof.append(df_oof)\n",
    "\n",
    "    pred_test = lgb_model.predict_proba(\n",
    "        test[feature_names], num_iteration=lgb_model.best_iteration_)\n",
    "    prediction[f'{ycol}_0'] += pred_test[:,0] / kfold.n_splits\n",
    "    prediction[f'{ycol}_1'] += pred_test[:,1] / kfold.n_splits\n",
    "\n",
    "    df_importance = pd.DataFrame({\n",
    "        'column': feature_names,\n",
    "        'importance': lgb_model.feature_importances_,\n",
    "    })\n",
    "    df_importance_list.append(df_importance)\n",
    "\n",
    "    del lgb_model, pred_val, pred_test, X_train, Y_train, X_val, Y_val\n",
    "    gc.collect()\n",
    "    \n",
    "    \n",
    "df_importance = pd.concat(df_importance_list)\n",
    "df_importance = df_importance.groupby(['column'])['importance'].agg(\n",
    "    'mean').sort_values(ascending=False).reset_index()\n",
    "df_importance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "f1: 0.835394862036156\n"
     ]
    }
   ],
   "source": [
    "df_oof = pd.concat(oof)\n",
    "score = f1_score(df_oof[ycol].astype('int'), \n",
    "                 np.argmax(df_oof[['label_0', 'label_1']].values, axis=1).astype('int'))\n",
    "print('f1:', score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>82750f1b9d1223508ee329d47e27d35176c93eb9f35e9c1a</td>\n",
       "      <td>0.05</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>f000950527a6feb670cc1c87c2025f3922aaa4a0206a0a33</td>\n",
       "      <td>0.52</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>e9f7b28ec10e04700ef4db75a494f9a1e8e8b09555e6afa1</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>beb4aaaa89e0a0ae9d77bd5d7665be6342f552f51840cf19</td>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>e9f7b28ec10e0470ee4172cec0133b6826c34f27d3dff204</td>\n",
       "      <td>0.08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 id  score\n",
       "0  82750f1b9d1223508ee329d47e27d35176c93eb9f35e9c1a   0.05\n",
       "1  f000950527a6feb670cc1c87c2025f3922aaa4a0206a0a33   0.52\n",
       "2  e9f7b28ec10e04700ef4db75a494f9a1e8e8b09555e6afa1   0.03\n",
       "3  beb4aaaa89e0a0ae9d77bd5d7665be6342f552f51840cf19   0.03\n",
       "4  e9f7b28ec10e0470ee4172cec0133b6826c34f27d3dff204   0.08"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub = prediction[['id', 'label_1']]\n",
    "sub.columns = ['id', 'score']\n",
    "\n",
    "sub = pd.merge(entprise_evaluate, sub, on='id', how='left')\n",
    "sub.drop(['label'], axis=1, inplace=True)\n",
    "\n",
    "sub.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub.to_csv(f'baseline_{score}.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
